package com.acane.instagram.mapper;

import com.acane.instagram.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserMapper extends BaseMapper<User> {

    

    @Select("select ur.recommend_id from user_recommend ur left join user u on ur.user_id = u.id where ur.user_id = #{recommendUserId}")
    List<String> getRecommendUserId(@Param("recommendUserId") String recommendUserId);

    @Select("<script>" +
            "SELECT * FROM user WHERE id IN " +
            "<foreach item='id' index='index' collection='recommendUserIds' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<User> getRecommendUserList(@Param("recommendUserIds") List<String> recommendUserIds);

    @Select("select status from follows where following_id = #{followingId} and follower_id = #{followerId}")
    Integer judgeFollowed(@Param("followingId") String followingId, @Param("followerId") String followerId);


    @Select("SELECT DISTINCT u.*" +
            " FROM user u" +
            " JOIN chat_messages cm ON u.user_name = cm.receiver OR u.user_name = cm.sender" +
            " WHERE (cm.sender = #{userName} OR cm.receiver = #{userName})" +
            " AND u.user_name != #{userName}")
    List<User> getMessageList(@Param("userName") String userName);


    @Select("SELECT COUNT(*) FROM post WHERE user_id = #{userId}")
    int getPostCount(String userId);

    @Select("SELECT COUNT(*) FROM follows WHERE following_id = #{userId}")
    int getFollowingCount(String userId);

    @Select("SELECT COUNT(*) FROM follows WHERE follower_id = #{userId}")
    int getFollowerCount(String userId);

    @Select("SELECT * FROM user " +
            "WHERE user_name LIKE CONCAT('%', #{keyword}, '%') " +
            "OR nick_name LIKE CONCAT('%', #{keyword}, '%') " +
            "OR introduction LIKE CONCAT('%', #{keyword}, '%')")
    List<User> searchUser(String keyword);
}
